// pages/login/login.js
var app = getApp()
var ddapi = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    verifyCodeTime: '获取验证码',
    avatarUrl:'',
    nickName:''
  },
  mobileInputEvent: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  bindSave: function (e) {
    var that = this;
    var mobile = e.detail.value.mobile;
    var code = e.detail.value.code;
    var avatarUrl = that.data.avatarUrl;
    var nickName = that.replace(that.data.nickName);
    if (mobile == "") {
      wx.showModal({
        title: '提示',
        content: '请填写手机号码',
        showCancel: false
      })
      return
    }
    if (code == "") {
      wx.showModal({
        title: '提示',
        content: '请填写验证码',
        showCancel: false
      })
      return
    }
    var sign = ddapi.md5(avatarUrl + code + nickName + mobile + 'User.SendLoginWx');
    console.log(sign)
    wx.request({
      url: app.globalData.apiUrl + '?service=User.SendLoginWx',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        reg_tel: mobile,
        captcha:code,
        avatar: avatarUrl,
        nickname: nickName
      },
      success: function (res) {
        console.log(res)
        if (res.data.data.code == 0) {
          wx.showToast({
            title: '绑定成功'
          })
          console.log(res.data.data.info.open_id)
          wx.setStorageSync('openid', res.data.data.info.open_id);
          wx.setStorageSync('user_id', res.data.data.info.user_id);
          wx.setStorageSync('userPhone', mobile);
          console.log(wx.getStorageSync('userPhone'))
          //修改登陆状态
          that.CheckLand(res.data.data.info);
          setTimeout(function () {
            var url = '/pages/map/map';
            wx.switchTab({
              url: url,
            });
          }, 1000);
        } else {
          wx.showToast({
            title: '绑定失败',
            image: '../../images/popup-close.png'
          })
        }
      }
    })
  },

  //过滤emoji表情符号
  replace: function (nickName) {
    return nickName.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
  },

  verifyCodeEvent: function (e) {
    if (this.data.buttonDisable) return false;
    var that = this;
    var c = 60;
    var intervalId = setInterval(function () {
      c = c - 1;
      that.setData({
        verifyCodeTime: c + 's后重发',
        buttonDisable: true
      })
      if (c == 0) {
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '获取验证码',
          buttonDisable: false
        })
      }
    }, 1000)
    var mobile = this.data.mobile;
    var regMobile = /^1\d{10}$/;
    if (!regMobile.test(mobile)) {
      wx.showToast({
        title: '手机号有误！',
        image: '../../images/popup-close.png'
      })
      return false;
    }
    var sign = ddapi.md5(mobile+'User.SendCaptcha');
    console.log(sign)
    wx.request({
      url: app.globalData.apiUrl + '?service=User.SendCaptcha',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        reg_tel: mobile
      },
      success: function (res) {
        if(res.data.data.code==0){
          that.setData({
            avatarUrl: e.detail.userInfo.avatarUrl,
            nickName: e.detail.userInfo.nickName,
          });
          wx.showToast({
            title: '发送成功'
          })

        }else{
          wx.showToast({
            title: '发送失败',
            image: '../../images/popup-close.png'
          })
        }
      }
    })

  },


  CheckLand: function (info) {
    var that = this;
    var service = 'User.CheckLand';
    var open_id = info.open_id;
    var user_id = info.user_id;
    var sign = ddapi.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.CheckLand',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 0) {
            that.editLand(info);
          } else {
            wx.switchTab({
              url: '/pages/map/map',
            });
          }
        }
      }
    })
  },



  editLand: function (info) {
    var service = 'User.EditLand';
    var open_id = info.open_id;
    var user_id = info.user_id;
    var pid = 1;
    var sign = ddapi.md5(open_id + pid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.EditLand',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        pid: pid,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 1) {
            wx.switchTab({
              url: '/pages/map/map',
            });
          } else {
            wx.showToast({
              title: '修改失败',
              icon: 'success',
              duration: 2000
            })
          }
        }
      }
    })
  }

  
})